package com.donfelix.animationdemo.animation;

import android.databinding.DataBindingUtil;
import android.transition.ChangeBounds;
import android.transition.Scene;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.donfelix.animationdemo.R;
import com.donfelix.animationdemo.common.BaseDetailActivity;
import com.donfelix.animationdemo.common.Sample;
import com.donfelix.animationdemo.databinding.ActivityAnimations2Binding;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Don on 2017/3/23.
 * Desc
 */

public class AnimationsActivity2 extends BaseDetailActivity implements View.OnClickListener {

    private static final int DELAY = 100;
    private Scene scene0;
    private Scene scene1;
    private Scene scene2;
    private Scene scene3;
    private Scene scene4;
    private List<View> viewsToAnimate = new ArrayList<>();
    private ActivityAnimations2Binding binding;
    private Sample sample;

    @Override
    protected void bindData() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_animations2);
        sample = (Sample) getIntent().getExtras().getSerializable(EXTRA_SAMPLE);
        binding.setAnimationsSample(sample);
    }

    @Override
    protected void setupWindowAinimation() {
        getWindow().setReenterTransition(TransitionInflater.from(this).inflateTransition(R.transition.slide_from_bottom));
        getWindow().getReenterTransition().addListener(new Transition.TransitionListener() {
            @Override
            public void onTransitionStart(Transition transition) {
            }

            @Override
            public void onTransitionEnd(Transition transition) {
                getWindow().getReenterTransition().removeListener(this);
                TransitionManager.go(scene0);
            }

            @Override
            public void onTransitionCancel(Transition transition) {
            }

            @Override
            public void onTransitionPause(Transition transition) {
            }

            @Override
            public void onTransitionResume(Transition transition) {
            }
        });
    }

    @Override
    protected void setupLayout() {
        final LinearLayout actRoot = binding.buttonsGroup;
        FrameLayout sceneRoot = binding.sceneRoot;
        scene0 = Scene.getSceneForLayout(sceneRoot, R.layout.activity_animations_scene0, this);
        scene0.setEnterAction(new Runnable() {
            @Override
            public void run() {
                int size = viewsToAnimate.size();
                for (int i = 0; i < size; i++) {
                    View child = viewsToAnimate.get(i);
                    child.animate()
                            .setStartDelay(i * DELAY)
                            .scaleX(1f)
                            .scaleY(1f);
                }
            }
        });
        scene0.setExitAction(new Runnable() {
            @Override
            public void run() {
                TransitionManager.beginDelayedTransition(actRoot);
                View title = scene0.getSceneRoot().findViewById(R.id.scene0_title);
                title.setScaleX(0f);
                title.setScaleY(0f);
            }
        });
        scene1 = Scene.getSceneForLayout(sceneRoot, R.layout.activity_animations_scene1, this);
        scene2 = Scene.getSceneForLayout(sceneRoot, R.layout.activity_animations_scene2, this);
        scene3 = Scene.getSceneForLayout(sceneRoot, R.layout.activity_animations_scene3, this);
        scene4 = Scene.getSceneForLayout(sceneRoot, R.layout.activity_animations_scene4, this);

        binding.sample3Button1.setOnClickListener(this);
        binding.sample3Button2.setOnClickListener(this);
        binding.sample3Button3.setOnClickListener(this);
        binding.sample3Button4.setOnClickListener(this);

        viewsToAnimate.add(binding.sample3Button1);
        viewsToAnimate.add(binding.sample3Button2);
        viewsToAnimate.add(binding.sample3Button3);
        viewsToAnimate.add(binding.sample3Button4);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sample3_button1:
                TransitionManager.go(scene1, new ChangeBounds());
                break;
            case R.id.sample3_button2:
                TransitionManager.go(scene2, TransitionInflater.from(AnimationsActivity2.this)
                        .inflateTransition(R.transition.slide_and_changebounds));
                break;
            case R.id.sample3_button3:
                TransitionManager.go(scene3, TransitionInflater.from(AnimationsActivity2.this)
                        .inflateTransition(R.transition.slide_and_changebounds_sequential));
                break;
            case R.id.sample3_button4:
                TransitionManager.go(scene4, TransitionInflater.from(AnimationsActivity2.this)
                        .inflateTransition(R.transition.slide_and_changebounds_sequential_with_interpolators));
                break;
            default:
                break;
        }
    }
}
